import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/utils/theme_util.dart';

class GradientButton extends StatefulWidget {
  ///按钮文字
  final String? text;
  TextStyle? textStyle;
  final double? width;
  final double? height;
  BorderRadius? radius;
  final List<Color>? colors;
  final Function()? onPressed;
  final Function(bool)? onFocusChange;
  final Function(bool)? onHover;
  final Function()? onLongPress;

  ///正常颜色
  late Color normalColor;

  ///按下时的颜色
  late Color pressColor;

  ///禁用时的颜色
  late Color disabledColor;

  ///禁用时的颜色
  late Color hoveredColor;

  ///获取焦点时的颜色
  late Color focusedColor;

  ///按下时的颜色
  late Color pressTextColor;

  ///按下时的颜色
  late Color normalTextColor;

  ///禁用时的颜色
  late Color disabledTextColor;

  ///禁用时的颜色
  late Color hoveredTextColor;

  ///获取焦点时的颜色
  late Color focusedTextColor;

  ///渐变色方向 true为水平,false为垂直
  final bool gradientIsHorizontal;

  ///主题
  final ThemeData themeData;

  final EdgeInsets padding;

  final EdgeInsets margin;

  Widget? child;

  BoxDecoration? decoration;

  GradientButton(
      this.themeData, {
        Key? key,
        this.colors,
        this.child,
        this.radius,
        this.gradientIsHorizontal = true,
        this.text,
        this.textStyle,
        this.width,
        this.height,
        this.onPressed,
        this.onFocusChange,
        this.onHover,
        this.onLongPress,
        this.padding = EdgeInsets.zero,
        this.margin = EdgeInsets.zero,
        this.decoration,
        Color? pressColor,
        Color? disabledColor,
        Color? hoveredColor,
        Color? focusedColor,
        Color? normalColor,
        Color? pressTextColor,
        Color? disabledTextColor,
        Color? hoveredTextColor,
        Color? focusedTextColor,
        Color? normalTextColor,
      }) : super(key: key) {
    this.pressColor = pressColor ?? themeData.pressColor();
    this.disabledColor = disabledColor ?? themeData.disableColor();
    this.hoveredColor = hoveredColor ?? themeData.hoveredColor();
    this.focusedColor = focusedColor ?? themeData.focusedColor();
    this.pressTextColor = pressTextColor ?? themeData.focusedTextColor();
    this.disabledTextColor = disabledTextColor ?? themeData.disabledTextColor();
    this.hoveredTextColor = hoveredTextColor ?? themeData.hoveredTextColor();
    this.focusedTextColor = focusedTextColor ?? themeData.focusedTextColor();
    this.normalColor = normalColor ?? themeData.colorScheme.primary;
    this.normalTextColor = normalTextColor ?? themeData.colorScheme.secondary;
    radius = radius ?? BorderRadius.circular(50);
    textStyle = textStyle ?? TextStyle(fontSize: 28.sp, color: themeData.thirdlyTextColor());
  }

  @override
  _GradientButtonState createState() => _GradientButtonState();
}

class _GradientButtonState extends State<GradientButton> {
  ///渐变色背景
  Gradient? get gradient {
    if (widget.colors != null) {
      if (widget.gradientIsHorizontal) {
        return LinearGradient(
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
          colors: widget.colors!,
        );
      } else {
        return LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: widget.colors!,
        );
      }
    } else {
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      height: widget.height,
      margin: widget.margin,
      decoration: widget.decoration ??
          BoxDecoration(
            borderRadius: widget.radius!,
            gradient: gradient,
          ),
      child: TextButton(
        style: ButtonStyle(
          minimumSize: MaterialStateProperty.all(Size.zero),
          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          padding: MaterialStateProperty.all(widget.padding),
          shape: shape,
          backgroundColor: MaterialStateProperty.resolveWith(
                (states) {
              if (states.contains(MaterialState.hovered)) {
                return widget.hoveredColor;
              } else if (states.contains(MaterialState.disabled)) {
                return widget.disabledColor;
              } else if (states.contains(MaterialState.focused) &&
                  !states.contains(MaterialState.pressed)) {
                return widget.focusedColor;
              } else if (states.contains(MaterialState.pressed)) {
                return widget.pressColor;
              } else {
                if (widget.decoration == null) {
                  return widget.colors != null
                      ? Colors.transparent
                      : widget.themeData.colorScheme.primary;
                } else {
                  return widget.normalColor;
                }
              }
            },
          ),
          foregroundColor: MaterialStateProperty.resolveWith(
                (states) {
              if (states.contains(MaterialState.hovered)) {
                return widget.hoveredTextColor;
              } else if (states.contains(MaterialState.disabled)) {
                return widget.disabledTextColor;
              } else if (states.contains(MaterialState.focused) &&
                  !states.contains(MaterialState.pressed)) {
                return widget.focusedTextColor;
              } else if (states.contains(MaterialState.pressed)) {
                return widget.pressTextColor;
              } else {
                return widget.normalTextColor;
              }
            },
          ),
        ),
        child: widget.child ??
            Text(
              widget.text ?? '',
              style: widget.textStyle,
            ),
        onPressed: widget.onPressed,
        onFocusChange: widget.onFocusChange,
        onHover: widget.onHover,
        onLongPress: widget.onLongPress,
      ),
    );
  }

  MaterialStateProperty<OutlinedBorder?>? get shape {
    return MaterialStateProperty.all(RoundedRectangleBorder(
        borderRadius: widget.decoration != null
            ? widget.decoration!.borderRadius as BorderRadius
            : widget.radius!));
  }
}
